jQuery(function($) {
  // Function to check if a CSS style is supported.
  function cssSupported(styleName, styles) {
    if (styles) {
      var n;
      while ((n = styleName.indexOf("-")) != -1) {
        styleName = styleName.substr(0, n) + styleName.substr(n + 1, 1).toUpperCase() + styleName.substr(n + 2);
      }
      return (typeof(styles[styleName]) != "undefined");
    }
    styles = document.createElement("div").style;
    var result = false;
    $.each(["","-webkit-","-moz-","-khtml-","-o-"], function() {
      result = result || cssSupported(this + styleName, styles);
    });
    return result;
  }

  // Function to apply inline styles if CSS selectors didn't work
  function setCssStyle(selector, styles) {
    var elts = $(selector);
    for (var styleName in styles) {
      if (elts.css(styleName) != styles[styleName]) {
        elts.css(styleName, styles[styleName]);
      }
    }
  }

  // Check what this browser supports
  var ie6 = ($.browser.msie && $.browser.version == "6.0");
  var ieBelow8 = ($.browser.msie && $.browser.version < "8");
  var quirksMode = ie6 || !$.support.boxModel;
  var cssBoxes = cssSupported("box-flex");
  var roundedCorners = cssSupported("border-radius");

  // Apply document styles
  quirksMode && $("body").addClass("quirksMode");
  !cssBoxes && $("body").addClass("noBoxes");

  // If browser doesn't support CSS3 selectors, apply inline styles
  setCssStyle(".fluidCol .section:first > H1", {"font-size": "2em"});

  // Webkit has a bit of a weirdness with margins.
  if (cssBoxes && $.browser.safari) {
    $(".fluidCol").css("padding-top", "1px");
  }

  // If browser doesn't support CSS3 floating box layout, emulate with floats instead.
  if (!cssBoxes) {
    $(".columns").each(function() {
      var $this = $(this);
      var $fluid = $this.children(".fluidCol");
      var $fixed = $this.children(".fixedCol");
      var fixedWidth = $fixed.css("width");
      $fluid.css({"margin-right":fixedWidth});
      $fixed.css({"float":"left", "margin-left":"-" + fixedWidth});
      $fluid.wrap($("<div />").css({"float":"left", "width":"100%"}));
    });
  }

  // If border-radius isn't supported, replace the CSS borders with margins
  // and superimpose graphical rounded borders instead. Not in IE6, PNG support too slow.
  if (!roundedCorners && !ie6) {
    $.fn.extend({
      sizeBorder: function() {
        return this.each(function() {
          var $this = $(this);
          var width = $this.outerWidth();
          var height = $this.outerHeight();
          var heightDelta = $this.hasClass("dgbNoTop") ? 14 : 28;
          $this.children(".dgbc").width(width - 28);
          $this.children(".dgbm").height(height - heightDelta);
          $this.children(".dgbr").css("margin-left", width - 14);
          $this.children(".dgbb").css("margin-top", height - 14);
        });
      }
    });

    $(".section").each(function() {
      var $section = $(this);
      var $wrapInner = $("<div></div>");
      var $wrapOuter = $("<div class='dgbOuter'></div>");
      if ($section.css("border-top-width") == "0px") {
        $wrapOuter.addClass("dgbNoTop");
      }
      $.each(["left","right","top","bottom"], function() {
        $wrapInner.css("padding-" + this, $section.css("border-" + this + "-width"));
        $wrapOuter.css("margin-" + this, $section.css("margin-" + this));
      });

      $section.css({"border":"0", "margin":"0"})
        .wrap($wrapInner)
          .parent()
        .wrap($wrapOuter)
        .before("<div class='dgb dgbt dgbl dgbtl' />")
        .before("<div class='dgb dgbt dgbc dgbtc' />")
        .before("<div class='dgb dgbt dgbr dgbtr' />")
        .before("<div class='dgb dgbl dgbm dgbml' />")
        .before("<div class='dgb dgbr dgbm dgbmr' />")
        .before("<div class='dgb dgbb dgbl dgbbl' />")
        .before("<div class='dgb dgbb dgbc dgbbc' />")
        .before("<div class='dgb dgbb dgbr dgbbr' />")
        .parent()
        .sizeBorder();
    });

    var $borders = $(".dgbOuter");
    var timeoutId = null;
    $(window).resize(function() {
      if (timeoutId) {
        window.clearTimeout(timeoutId);
      }
      timeoutId = window.setTimeout(function() { timeoutId = null; $borders.sizeBorder(); }, 1);
    });
  }

   // Fix z-index bug for floating menus
  if (ieBelow8) {
    $(".DDRMenuContent")
      .parents(".section")
      .css("z-index", 1)
      .parents(".dgbOuter")
      .css("z-index", 1);
  }
});
